<template>
    <div class="root" style="height:100vh;width:100vw;">
        <el-scrollbar style="height: 100%;width: 100%">
            <div class="index-page">
                <div class="page-center page-nav">
                    <div class="page-center-content">
                        <div class="nav-left">
                            <span class @click="toJob">进入个人版</span>
                        </div>
                        <div class="nav-right">
                            <el-dropdown trigger="click" @command="handleCommand">
                                <div style="display:flex;align-items:center;">
                                    <img :src="company.headImage" alt>
                                    <span>{{company.fullName}}<i class="el-icon-caret-bottom" style="color:#fff"></i>
                                    </span>
                                </div>
                                <el-dropdown-menu slot="dropdown">
                                    <el-dropdown-item command="1">信息设置</el-dropdown-item>
                                    <el-dropdown-item command="2">账号设置</el-dropdown-item>
                                    <el-dropdown-item command="3">退出登录</el-dropdown-item>
                                </el-dropdown-menu>
                            </el-dropdown>
                        </div>
                    </div>
                </div>
                <div class="page-center page-navs">
                    <div class="page-center-content">
                        <a href="/index"><img src="../assets/img/register/logo.png" class="navs-logo" alt></a>
                        <el-menu mode="horizontal" class="nvas-menu">
                            <el-menu-item
                                    v-for="(item, index) in navsList"
                                    :key="index">
                                <router-link :to="item.to" @click.native="synState(index)">{{item.label}}</router-link>
                            </el-menu-item>
                        </el-menu>
                        <el-button class="company-post-job" @click="doPostJob">发布职位</el-button>
                    </div>
                </div>
                <div class="page-content">
                    <!--<keep-alive :include="/^index-/">-->
                    <router-view></router-view>
                    <!--</keep-alive>-->
                </div>
                <!--未完善信息的提示框-->
                <el-dialog class="improve_dialog" :visible.sync="showImproveQ" title="提示">
                    <ImproveInfo :which="showWhich" @not-now="showImproveQ=false" @edit-now="editNow"></ImproveInfo>
                </el-dialog>
            </div>
        </el-scrollbar>
    </div>
</template>

<script>
  import LeftNav from '../components/leftnav.vue'
  import _subject from '../assets/utils/subject'
  import * as _ from 'underscore'
  import _g from '../assets/utils/global'
  import ImproveInfo from './setting/ImproveInfo'

  export default {
    data() {
      return {
        index: true, //用来控制尾部是否显示
        activeIndex: '-1',
        navsList: [
          { label: '首页', to: '/index' },
          { label: '简历管理', to: '/resume' },
          { label: '沟通', to: '/communication' },
          { label: '职位管理', to: '/job' }
        ],
        company: {},
        subscription: null,
        showImproveQ: false,
        showWhich: ''
      }
    },
    methods: {
      initData() {
        let userInfo = JSON.parse(localStorage.getItem(localSaved.userInfo))
        console.log('init', userInfo)
        if (!userInfo || !userInfo.hasOwnProperty('company')) {
          this.$router.push('/login')
        }
        this.company = userInfo.company
        this.company.headImage = _subject.getCompanyIcon()
        console.log(userInfo)
      },
      toJob() {
        window.location.href = 'http://talent.gcs360.com'
      },
      synState(index) {
        if (index == 1) {
          _g.getCache().gc.g = 0
          _g.getCache().gc.c = 0
        } else if (index == 2) {
          _g.getCache().gc.g = 1
          _g.getCache().gc.c = 0
        }
        //console.log(index)
      },
      jumpNav(item, index) {
        this.index = true
        this.activeIndex = index
        this.$router.push({ path: item.to })
      },
      handleCommand(command) {
        //点击下拉菜单
        this.activeIndex = -1
        if (command == 1) {
          //点击账号设置
          this.index = false
          _g.getCache().sIndex = 0
          _subject.setting.next(0)
          this.$router.push({ path: '/setting/personal' })
        } else if (command == 2) {
          //点击银行账号
          this.index = false
          _g.getCache().sIndex = 2
          _subject.setting.next(2)
          this.$router.push({ path: '/setting/binding' })
        } else {
          this.$router.push({ path: '/login' })
          _g.apiPost('/company/logout.do', {}).then(res => {
            _g.toMessage(res)
            if (res.status == 0) {
              localStorage.removeItem(localSaved.userInfo)
              this.$router.push({ path: '/login' })
            }
          })
        }
      },
      doLogin() {
        //跳转到登录页面
        this.$router.push({ path: '/login' })
      },
      doRegister() {
        //跳转到注册页面
        this.$router.push({ path: '/register' })
      },
      doPostJob() {
        if (_subject.personInfoQ() && _subject.companyInfoQ()) {
          //跳转到发布职位页面
          this.activeIndex = -1
          this.$router.push({ path: '/postjob' })
        } else {
          this.showImproveQ = true
          if (!_subject.personInfoQ()) {
            this.showWhich = '企业信息'
            _subject.setting.next(0)
            return
          }
          // if (!_subject.companyInfoQ()) {
          //   this.showWhich = '企业信息'
          //   _subject.setting.next(1)
          // }
        }
      },
      editNow() {
        console.log('edit now')
        this.activeIndex = -1
        this.showImproveQ = false
        this.$router.push({ path: '/setting/personal' })
      }
    },
    created() {
      //用于和子组件之间联动
      let self = this
      this.subscription = _subject.page.subscribe({
        next: function(t) {
          console.log('view index subject')
          console.log(t)
          if (!_.isUndefined(t)) {
            self.activeIndex = t
            self.initData()
          }
        }
      })
      this.initData()
    },
    mounted() {
      window.addEventListener('beforeunload', e => {
        console.log('before remove', e)
        // window.localStorage.removeItem(localSaved.userInfo)
      })
    },
    destroyed() {
      if (!_.isNull(this.subscription)) {
        this.subscription.unsubscribe()
      }
    },
    components: { ImproveInfo, LeftNav }
  }
</script>

<style lang="scss" scoped>
    .page-content .center-content {
        margin: auto auto;
        margin-top: 20px;
        width: 1201px;
        height: 1062px;
        position: relative;
    }

    .page-content .center-content .left {
        position: absolute;
        top: 0px;
    }

    .improve_dialog {

        padding: 0;

        .el-dialog {
            .el-dialog__body {
                padding: 0;
            }
        }
    }

</style>
